<br />
<div id="sortselection">	
	<?php   
    echo $this->Form->create('Event', array('url' => array_merge($this->params['named'], array('controller' => 'events', 'action' => 'index'))));
    echo $this->Form->label('Sort Events By: ');
    echo $this->Form->select('sort_type', array(
                                'oldest' => 'First Added',
                                'newest' => 'Last Added',
				'dateasc' => 'First Event Date',
				'datedesc' => 'Last Event Date'),
                                null, array('empty' => false, 'onchange' => 'this.form.submit();'));
    echo $this->Form->end();
    ?>
</div>
<?php 
	if ($hasFilter) { echo $html->link('Return to events page', '/events').'<h2>Search Results for "'.$this->params['named']['query'].'"</h2><br />'; } 
	else { echo '<h2>All Events</h2><br />'; } 

	$hostname = Configure::read('Sfp.hostname'); 

	$entries_per_row = 4;
	$thumbs_per_event = 4;

	if ($hasFilter) {
		$noResult = true;
		foreach ($famArray as $family) {
			if (!empty($family['Events'])) {
				$noResult = false;
				break;
			}
		}

		if ($noResult) {
			echo '<br /><p>Sorry - no matches.  Try again using a different keyword, or click ';
			echo $this->Html->link('here', '/events/index', array('class' => 'button'));
			echo ' to view all events.</p>';
		}
	}
?>


<script type="text/javascript">

	var theHREF;

	function updateHref(href) {
		theHREF = href;
	}

	var $dialog_event_delete = $j('<div></div>')
		.html('Are you sure you want to delete this event?  All photos in this event will be deleted.')
		.dialog({
			autoOpen: false,
			title: 'Confirm event delete',
			modal: true,
			draggable: false,
			resizable: false,
			show: 'slide',
			hide: 'fadeout',
			buttons: { "Yes": function() { window.location.href = theHREF; $dialog_event_delete.dialog("close"); },
					   "No": function() { $dialog_event_delete.dialog("close"); }
					 }
		});
		var $dialog_photo_delete = $j('<div></div>')
		.html('Are you sure you want to delete all photos in this event?')
		.dialog({
			autoOpen: false,
			title: 'Confirm photo delete',
			modal: true,
			draggable: false,
			resizable: false,
			show: 'slide',
			hide: 'fadeout',
			buttons: { "Yes": function() { window.location.href = theHREF; $dialog_photo_delete.dialog("close"); },
					   "No": function() { $dialog_photo_delete.dialog("close"); }
					 }
		});

</script>


<?php
	$family_count = 0;
	foreach ($famArray as $family):
		// don't show empty families for search results
		if (empty($family['Events']) && $hasFilter) {
			continue;
		} ?>
	    
	    <h3 id="family_header<?php echo $family_count; ?>" class="family_header clear">
	    	<button id="toggleEvents<?php echo $family_count ?>">&nbsp;Hide&nbsp;</button>
			<?php echo $family['Family']['name']; ?>
		<?php if($family['Family']['is_admin']){ ?>
			<span style="font-size: .75em;">[<i><?php echo $html->link('edit', '/families/manage/family:'.$family['Family']['id']); ?></i>]</span>
		<?php } ?>
		</h3>
		<script>
			$j("#toggleEvents<?php echo $family_count ?>").click(function () {
				$j("#toggleEvents<?php echo $family_count ?>").html(
					$j("#familyEvents<?php echo $family_count ?>").is(':visible') ? "&nbsp;Show&nbsp;" : "&nbsp;Hide&nbsp;"
				);
				$j("#familyEvents<?php echo $family_count ?>").slideToggle("slow");
			});
		</script>
		<div id="familyEvents<?php echo $family_count ?>">
		<?php
		if (empty($family['Events']) && !$hasFilter) { ?>
			<div id="eventcover00" class="eventcover eventcoveradd span-5 push-1 prepend-top append-bottom">
				<div class="add_event">
				<?php echo $html->link($html->image('add.png', array('alt' => 'Add event')).'<p><big><strong>Add an event album</strong></big></p>', 
									   '/events/create/family:'.$family['Family']['id'],
									   array('title' => 'Start a new event album', 'escape' => false)); ?>
				</div>		
			</div>
		</div><!-- familyEvents<?php echo $family_count ?> -->
		<br /><br /> <?php
	$family_count++;
			continue;
        } 

	   	$events = $family['Events']; 
	    
		$column = 1;
		$event_count = 0;
		
		foreach ($events as $event): ?>
			<div id="eventcover<?php echo $family_count.$event_count; ?>" class="eventcover span-5 push-<?php echo $column; ?> prepend-top append-bottom <? if ($column % $entries_per_row == 0) echo 'last'; ?>">
			<div id="event_edit_wrapper<?php echo $family_count.$event_count; ?>" class="event_edit_wrapper">			
				<?php echo $html->link('edit', '#', array('id' => 'event_edit'.$family_count.$event_count)); ?>	
			</div>
			<?php
				$event_name = htmlspecialchars($event['Event']['name'], ENT_QUOTES);	// for edit box title text
				if (strlen($event['Event']['name']) > 12) {
			        $event_name = htmlspecialchars(substr($event['Event']['name'], 0, 12), ENT_QUOTES).'...';
				}
                                if($eventsAdminFlags[$event['Event']['id']]): ?>
			<script type="text/javascript">			
				
				var event_edit_html = 
									'<strong><?php echo htmlspecialchars($event["Event"]["name"], ENT_QUOTES); ?></strong><br /\>' +
									'<em><?php echo $event["Event"]["eventdate"]; ?></em><br /\>' +
									'<ul>' +
									'<li><?php echo $html->link("share event", "/events/share/event:".$event["Event"]["id"], array("id" => "event_share".$family_count.$event_count)); ?></li>' +
						      		//'<li><?php echo $html->link("rename event", "/events/rename/".$event["Event"]["id"], array("id" => "event_rename".$family_count.$event_count)); ?></li>' +
									//'<li><?php echo $html->link("change event date", "/events/changedate/".$event["Event"]["id"], array("id" => "event_change_date".$family_count.$event_count)); ?></li>' +
									'<li><?php echo $html->link("delete event", "/events/delete/".$event["Event"]["id"], array("id" => "event_delete_confirmation".$family_count.$event_count)); ?></li>' +	
									//'<li><?php echo $html->link("delete photos", "/events/view/event:".$event["Event"]["id"]."/page:1"); ?></li>' +
									'<li><?php echo $html->link("delete all photos in event", "/events/deleteAllPhotos/".$event["Event"]["id"], array("id" => "photo_delete_confirmation".$family_count.$event_count)); ?></li>' +
									'</ul>';
							
				var $dialog_event_edit<?php echo $family_count.$event_count; ?> = $j('<div></div>')
					.html(event_edit_html)
					.dialog({
						autoOpen: false,
						title: 'Edit event "<?php echo $event_name; ?>"',
						modal: true,
						draggable: false,
						resizable: false,
						width: 230,
						show: 'slide',
						hide: 'fadeout',						
						buttons: { "Dismiss": function() { $dialog_event_edit<?php echo $family_count.$event_count; ?>.dialog("close"); } }
					});				
				
				$j("#eventcover<?echo $family_count.$event_count; ?>").mouseenter(function() {
					$j("#event_edit_wrapper<?php echo $family_count.$event_count; ?>").show();
				});
				$j("#eventcover<?echo $family_count.$event_count; ?>").mouseleave(function() {
					$j("#event_edit_wrapper<?php echo $family_count.$event_count; ?>").hide();
				});

				$j("#event_edit<?php echo $family_count.$event_count; ?>").click(function() {	
					/* 					
					var position =  $j("#eventcover<?echo $family_count.$event_count; ?>").position();
					$dialog_event_edit<?php echo $family_count.$event_count; ?>.dialog("option", "position", [position.left + 220, position.top]);			
					*/
					$dialog_event_edit<?php echo $family_count.$event_count; ?>.dialog('open');
					// prevent the default action, e.g., following a link
					return false;
				});


				$j("#event_delete_confirmation<?php echo $family_count.$event_count; ?>").click(function() {
					updateHref($j(this).attr("href"));
					$dialog_event_delete.dialog('open');
					// prevent the default action, e.g., following a link
					return false;
				});
				
				$j("#photo_delete_confirmation<?php echo $family_count.$event_count; ?>").click(function() {
					updateHref($j(this).attr("href"));
					$dialog_photo_delete.dialog('open');
					return false;
				});
<?php endif; ?>
			</script>
			<div>&nbsp;
			<!--<span><big><strong><?php echo $event['Event']['name']; ?></strong></big></span>-->
			<?php 
			    $event_name = htmlspecialchars($event['Event']['name'], ENT_QUOTES);
			    if (strlen($event['Event']['name']) > 20) {
			        $event_name = htmlspecialchars(substr($event['Event']['name'], 0, 20), ENT_QUOTES).'...';
			    }
			    echo $html->link('<span><big><strong>'.$event_name.'</strong></big></span>', 
				  	            array('controller' => 'events', 'action' => 'view', 'event' => $event['Event']['id'], 'page' => 1), array('title' => htmlspecialchars($event['Event']['name'], ENT_QUOTES), 'class' => 'eventLink', 'escape' => false)); 
			?>
			</div>
			<div style="clear: both;">
				<span class="eventLinkDate">&nbsp;&nbsp;<?php echo $event['Event']['eventdate']; ?></span>
				<span class="eventLinkPhotoCount"><?php echo $event['Event']['count']; ?> photos&nbsp;&nbsp;</span>
			</div>
            <?php 
				$count = 0; 
				foreach($thumarr[$event['Event']['id']] as $thumlist): ?>
		            <div class="eventthumb">
		            <?php $count++; echo $html->image($hostname.'pix/phpThumb/phpThumb.php?src='.$hostname.'pix/'.$event['Event']['id'].'/'.$thumlist['file_name'].'&w=76&h=45&zc=C', array('width' => '76px', 'height' => '45px')); ?>
		            </div>
		    	<?php endforeach; ?>
	    	<?php for ($i = $count; $i < count($thumarr[$event['Event']['id']]); $i++): ?> <div class="eventthumb"></div> <?php endfor; ?>
			</div>

			<? $column++; 
	   		if ($column > $entries_per_row) $column = 1;

			$event_count++;
		endforeach;
		
		if (!$hasFilter) { ?>
			<div id="eventcover<?php echo $family_count.$event_count; ?>" class="eventcover eventcoveradd span-5 push-<?php echo $column; ?> prepend-top append-bottom <? if ($column % $entries_per_row == 0) echo 'last'; ?>">
				<div class="add_event">
				<?php echo $html->link($html->image('add.png', array('alt' => 'Add event')).'<p><big><strong>Add an event album</strong></big></p>', 
									   '/events/create/family:'.$family['Family']['id'],
									   array('title' => 'Start a new event album', 'escape' => false)); ?>
				</div>
			</div>
		<?php } ?>
		</div><!-- familyEvents<?php echo $family_count ?> -->
		<br /><br /> <?php
	$family_count++;
	endforeach;
?>


